using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;

public class BurinSkill0Bullet : Bullet
{
    private GameObject target;
    float spriteHalfSize;

    public void Init(SkillData data, GameObject target, float speed = 10, float lifeTime = 5f)
    {
        base.Initialize(data);

        this.target = target;
        this.speed = speed;

        transform.position = srcData.Source.transform.position + playerSpriteOffset;

        if (!target.CompareTag("Destructible")) spriteHalfSize = 0.9f;
        else spriteHalfSize = 0;

        SetLifeTime(lifeTime);
    }

    void Update()
    {
        Vector2 direction = target.transform.position - transform.position + new Vector3(0, spriteHalfSize);
        float distance = direction.magnitude;

        if (!target || !target.activeSelf || target.GetComponent<Damageable>().curHealth <= 0 || distance < 0.3) Destroy();

        Vector3 move = speed * Time.deltaTime * (Vector3)direction.normalized;
        if (move.magnitude >= distance) transform.position = target.transform.position + new Vector3(0, spriteHalfSize);
        else transform.position += move;

        SubSprite.transform.right = direction;
    }


    public override void Damage(GameObject tarObj, Vector3 damagePos)
    {
        if (tarObj == target && tarObj.TryGetComponent(out Damageable tar))
        {
            //Debug.Log(tarObj.name);
            owner.ApplyDamage(tar, srcData, true);
            Destroy();
        }
    }
}
